package cn.orange.ch03_linked;

import cn.orange.ListNode;

/**
 * LC141.环形链表
 给你一个链表的头节点 head ，判断链表中是否有环
 */
public class LC141 {
    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }
    public static void main(String[] args) {
        LC141 alg = new LC141();
        ListNode n1 = new ListNode(3);
        ListNode n2 = new ListNode(2);
        n1.next = n2;
        ListNode n3 = new ListNode(0);
        n2.next = n3;
        ListNode n4 = new ListNode(-4);
        n3.next = n4;
        n4.next = n2;
        System.out.println(alg.hasCycle(n1));
    }
}
